package ControlLayer;
import ModelLayer.Item;
import ModelLayer.StorageContainer;

/**
 * Storage Controller:
 * Connects the StorageUI to the model layer.
 * 
 * @author Gruppe 3
 * @version 1.0
 */
public class StorageCtr
{
    private StorageContainer storageContainer;
    
    /**
     * constructor
     */
    public StorageCtr()
    {   
        storageContainer = StorageContainer.getInstance();
    }
    
    /**
     * Creates an item and uses the method addItem to add it
     * @param itemCode  id / code of the item
     * @param name      name of the item
     * @param type      which type is the item (example: Hammer)
     * @param price     what price the item is being sold at (example: 100)
     * @param discount  how many procent of the price will taken away(example: 20% = 0.20)
     */
    public void createItem(String itemCode, String name, String type, double price, double discount)
    {
        Item newItem = new Item(itemCode, name, type, price, discount);
        addItem(newItem);
    }
    
    /**
     * Adds an item to the the ArrayList items 
     * @param item  item object to be added
     */
    public void addItem(Item item)
    {   
        if (item != null)
        {
            storageContainer.addItem(item.getItemCode(), item.getName(), item.getType(), item.getPrice(), item.getDiscount());
        }                               
    }
    
    /**
     * Checks for a wanted item in the ArrayList items 
     * @param searchItemCode id / code of the wanted item
     */
    public void findItem(String searchItemCode)
    {
        storageContainer.findItem(searchItemCode);
    }
    
    /**
     * Deletes a wanted item in the ArrayList items 
     * @param  searchItemCode the id / code of the wanted item
     */
    public void deleteItem(String searchItemCode)
    {
        storageContainer.deleteItem(searchItemCode);
    }
    
    /**
     * Updates an item in the ArrayList items
     * If the specific update input parameter is emptry, it doesnt change the field
     * @param oldItemCode   old id / code of the wanted item
     * @param itemCode      new id / code of the wanted item    
     * @param name          new name of the item
     * @param type          new type of the item (example: Hammer)
     * @param price         new price of the item, what it will be sold at (example: 100)
     * @param discount      new discount of item, how many procent of the price will taken away(example: 20% = 0.20)    
     */
    public void updateItem(String oldItemCode, String itemCode, String name, String type, double price, double discount)
    {   
        storageContainer.updateItem(oldItemCode, itemCode, name, type, price, discount);
    }
    
    public void listItem(String searchItemCode)
    {
        storageContainer.listItem(searchItemCode);
    }
    
    /**
     * prints all item i of ArrayList items in the storageContainer as output to the console
     */    
    public void listAllItems()
    {
        storageContainer.listAllItems();
    }
    
    /**
     *Finds a wanted item and returns it as output parameter
     *@param searchItemCode id / code of the wanted item
     *@return found item, otherwise it returns null
     */    
    public Item getItem(String searchItemCode)
    {
        Item foundItem = StorageContainer.getInstance().getItem(searchItemCode);
        
        if (foundItem != null)
        {
            return foundItem;
        }
        
        return null;    
    }   
}